package com.skeryl.sqlite.activity;

import com.skeryl.sqlite.dao.ChapterDAO;
import com.skeryl.sqlite.model.Chapter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class ChapterActivity extends Activity
{
	private TextView textshow;
	private ChapterDAO chapterDAO;
	private String sid, content;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 不显示系统的标题栏
		setContentView(R.layout.chapter);

		Bundle bundle = getIntent().getExtras();

		content = bundle.getString("content");
		sid = bundle.getString("sid");

		textshow = (TextView) findViewById(R.id.textshow);

		textshow.setText(content);
		textshow.setMovementMethod(ScrollingMovementMethod.getInstance());

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.chapter, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item)
	{

		chapterDAO = new ChapterDAO();
		Chapter chapter;
		switch (item.getItemId())
		// 得到被点击的item的itemId
		{
		case R.id.index_chapter:// 这里的Id就是布局文件中定义的Id，在用R.id.XXX的方法获取出来
			Log.v("menu", "index_chapter");

			Intent it = new Intent(ChapterActivity.this, MainActivity.class);

			startActivity(it);

			break;
		case R.id.next_chapter:
			Log.v("menu", "next_chapter");

			chapterDAO.closeAddGet();
			chapter = chapterDAO.find(Integer.toString(Integer.parseInt(sid) + 1));
			sid = Integer.toString(Integer.parseInt(sid) + 1);
			content = chapter.getContent();
			chapterDAO.close();
			
			textshow.scrollTo(0, 0);// 滚动到开头
			
			textshow.setText(content);
			
			textshow.setMovementMethod(ScrollingMovementMethod.getInstance());

			break;
		case R.id.pre_chapter:
			Log.v("menu", "pre_chapter");

			chapterDAO.closeAddGet();
			chapter = chapterDAO.find(Integer.toString(Integer.parseInt(sid) - 1));
			sid = Integer.toString(Integer.parseInt(sid) - 1);
			content = chapter.getContent();
			chapterDAO.close();
			
			textshow.scrollTo(0, 0);// 滚动到开头
			
			textshow.setText(content);
			
			textshow.setMovementMethod(ScrollingMovementMethod.getInstance());
			break;
		}
		return true;
	}
}
